package com.huang.foundation.service.impl;

import com.huang.utils.RedisUtil;
import com.huang.utils.entity.enums.RedisKeyPrefixes;
import com.huang.foundation.service.CaptchaService;
import com.huang.utils.CaptchaUtil;
import com.huang.utils.entity.enums.CaptchaType;
import com.huang.utils.entity.to.CaptchaTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author HuangShen
 * @Description
 * @create 2021-10-17 19:50
 */
@Service("captchaService")
public class CaptchaServiceImpl implements CaptchaService {


    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, Object> myRedisTemplate) {
        this.redisTemplate = myRedisTemplate;
    }


    @Override
    public CaptchaTO creatCaptcha(String ip) {
        CaptchaTO captchaTO = CaptchaUtil.creatCaptcha(CaptchaType.ArithmeticCaptcha.getCode());
        this.redisTemplate.opsForValue().set(RedisUtil.createKey(RedisKeyPrefixes.CaptchaKeyString, ip), captchaTO.getText());
        return captchaTO;
    }
}
